<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\company\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class PositionController extends AdminBaseController
{
    /**
     * 职位列表
     */
    public function index()
    {
        $Position = Db::name("position_lists");

        $lists = $Position->where("1=1")->order("id", "DESC")->select()->toArray();

        $this->assign('lists', $lists);

        return $this->fetch();
    }

    /**
     * 添加职位
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加职位提交
     */
    public function addPost()
    {
        $param = $this->request->param();

        if (empty($param['name'])) {
            return $this->error('请输入职位名称');
        }

        $data = [];
        $data['name'] = $param['name'];
        $data['status'] = empty($param['status']) ? 0 : $param['status'];

        $Position = Db::name("position_lists");
        $res = $Position->insert($data);
        if ($res === false) {
            return $this->error('添加失败!');
        }

        return $this->success('添加成功!', url('Position/index'));
    }

    /**
     * 编辑职位
     */
    public function edit()
    {
        $id = $this->request->param('id', 0);
        if ($id == 0) {
            $this->error('操作错误!');
        }

        $Position = Db::name("position_lists");

        $this_page = $Position->where("id", $id)->find();

        $this->assign('this_page', $this_page);

        return $this->fetch();
    }

    /**
     * 编辑职位提交
     */
    public function editPost()
    {
        $id = $this->request->param("id", 0);
        if ($id == 0) {
            return $this->error('获取信息失败，请刷新重试');
        }

        $param = $this->request->param();
        if (empty($param['name'])) {
            return $this->error('请输入职位名称');
        }

        $data = [];
        $data['name'] = $param['name'];
        $data['status'] = empty($param['status']) ? 0 : $param['status'];

        $Position = Db::name("position_lists");
        $res = $Position->where("id", $id)->update($data);
        if ($res === false) {
            return $this->error('操作失败!');
        }

        return $this->success('操作成功!', url('Position/index'));
    }

    /**
     * 删除职位
     */
    public function delete()
    {
        $id = $this->request->param("id", 0);
        if ($id == 0) {
            return $this->error('获取信息失败，请刷新重试');
        }

        $User = Db::name("user");
        $count = $User->where("position_id", $id)->count();
        if ($count > 0) {
            return $this->error('当前职位还有成员，删除失败！');
        }

        Db::name("position_lists")->where("id", $id)->delete();

        return $this->success('删除成功!', url('Position/index'));
    }
}
